/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package info.hengha.blue.action;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import info.hengha.blue.dao.NodeDao;
import info.hengha.blue.model.DateSerializer;
import info.hengha.blue.model.Node;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

/**
 *
 * @author Administrator
 */
public class NodesServlet extends HttpServlet {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
    private NodeDao nodeDao;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config); //To change body of generated methods, choose Tools | Templates.
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
                config.getServletContext());
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(Date.class, new DateSerializer());
        Gson gson = gsonBuilder.create();
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        //response.setCharacterEncoding("UTF-8");
        PrintWriter pw = response.getWriter();
        String idParameter = request.getParameter("id");
        int size = 15;
        int id = 0;
        if (idParameter != null) {
            id = Integer.parseInt(idParameter);
        }
        if (id > 0) {
            Node node = nodeDao.getNode(id);
            pw.append(gson.toJson(node));
        } else {
            String randomParameter = request.getParameter("random");
            boolean random = false;
            if (randomParameter != null) {
                random = Integer.parseInt(randomParameter) == 1;
            }

            if (random) {
                pw.append(gson.toJson(nodeDao.getRandomNodes(size)));
            } else {
                String pageParamter = request.getParameter("page");
                int page = 1;
                if (pageParamter != null) {
                    page = Integer.parseInt(pageParamter);
                }
                List<Node> nodes = nodeDao.getNodes(page, size);
                gson.toJson(nodes, pw);
            }
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    /**
     * @param nodeDao the nodeDao to set
     */
    public void setNodeDao(NodeDao nodeDao) {
        this.nodeDao = nodeDao;
    }

}
